﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using WBGModel;
using WBGBusiness;
using RecursosHumanos;

namespace Clinica
{
    public partial class frmExame3C_ : Form
    {
        Request request;
        ModelExame exame = new ModelExame();

        public frmExame3C_(Request req, int id)
        {
            exame.idExame = id;
            request = req;
            InitializeComponent();
        }

        private void frmPessoa_Load(object sender, EventArgs e)
        {
            try
            {
                if (request == Request.Inserir)
                {
                    PreparaInsert();
                }
                else
                {
                    CarregaCampos();
                }
            }
            catch (Exception ex)
            {
                
            }            
        }

        private void PreparaInsert()
        {
            cbSexo.Text = "Ambos";
            chkAtivo.Checked = true;
        }

        private void CarregaCampos()
        {
            exame = new BusExame().Listar("Exame.idExame = " + exame.idExame.ToString())[0];
            txtIDExame.Text = exame.idExame.ToString();
            txtNome.Text = exame.nome;
            txtObservacao.Text = exame.observacao;
            chkExigeAcompanhante.Checked = exame.exigeAcompanhante;
            chkAtivo.Checked = exame.ativo;

            switch (exame.sexoAplicavel)
            {
                case "A":
                    cbSexo.Text = "Ambos";
                    break;
                case "M":
                    cbSexo.Text = "Masculino";
                    break;
                case "F":
                    cbSexo.Text = "Feminino";
                    break;
                default:
                    break;
            }
        }

        private void CarregaRegistro()
        {
            try
            {
                exame.nome = txtNome.Text;
                exame.observacao = txtObservacao.Text;
                exame.sexoAplicavel = cbSexo.Text.Substring(0, 1);
                exame.exigeAcompanhante = chkExigeAcompanhante.Checked;
                exame.ativo = chkAtivo.Checked;
            }
            catch
            {
                throw new Exception("Há dados inválidos.");
            }
        }

        private void frmLocacao__KeyUp(object sender, KeyEventArgs e)
        {
            switch(e.KeyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.F5:
                    try
                    {
                        CarregaRegistro();
                        if (request == Request.Inserir)
                        {
                            new BusExame().Inserir(exame);
                        }
                        else
                        {
                            new BusExame().Alterar(exame);
                        }
                        MessageBox.Show("Registro gravado com sucesso.", "Concluído", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.InnerException.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    break;
            }
        }
    }
}
